package com.java.io;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.SortedMap;

public class StreamClient {

	public static void main(String[] args) throws Exception {
		charset();
		//Properties p = System.getProperties();
		//p.list(System.out);
	}

	//字符集
	public static void charset() throws Exception {
		SortedMap<String,Charset> map = Charset.availableCharsets();
		Set<String> set = map.keySet();
		for(Iterator<String> it = set.iterator();it.hasNext();){
			String key = it.next();
			Charset charset = map.get(key);
			System.out.println(key + "----------------------" + charset.displayName());
		}

	}

	public static void Inf() throws Exception {
		InputStream in = new FileInputStream("source/ShortcutKeys.txt");
		byte[] b = new byte[200];
		int length = 0;
		while(-1!=(length = in.read(b,0,200))){
			String s = new String(b,0,length);
			System.out.println(s);
		}
		in.close();
	}

	public static void fileout() throws Exception {
		OutputStream out = new FileOutputStream("source/out.txt");
		String str = "helloworld";
		byte[] buffer = str.getBytes();
		out.write(buffer);
		out.close();
	}

	public static void bufferfileout() throws Exception {
		OutputStream out = new BufferedOutputStream(new FileOutputStream("source/out.txt"));
		String str = "helloworld";
		byte[] buffer = str.getBytes();
		out.write(buffer);
		out.flush();
		out.close();
	}

	public static void bytearrayout() throws Exception{
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		String str = "abc";
		byte[] bytes = str.getBytes();
		bout.write(bytes);

		OutputStream out = new FileOutputStream("source/out.txt");
		bout.writeTo(out);
		out.close();
		bout.close();
	}

	public static void dataout() throws Exception{
		DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("source/out.txt")));
		dos.writeDouble(3.5);
		dos.writeInt(5);
		dos.close();

		DataInputStream dis = new DataInputStream(new FileInputStream("source/out.txt"));
		double a = dis.readDouble();
		System.out.println(a);
		int b = dis.readInt();
		System.out.println(b);
		dis.close();
	}

	public static void writers() throws Exception{
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("source/out.txt")));
		writer.write("你好啊!");
		writer.newLine();
		writer.write("我很好!");
		writer.close();

		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("source/out.txt")));
		String ling = "";
		while(null != (ling = reader.readLine())){
			System.out.println(ling);
		}
		reader.close();
	}

	public static void readsys() throws Exception{
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String ling = "";
		while(null != (ling = reader.readLine())){
			System.out.println(ling);
		}
		reader.close();
	}

	public static void filereader() throws Exception{
		BufferedReader reader = new BufferedReader(new FileReader("source/out.txt"));
		String ling = "";
		while(null != (ling = reader.readLine())){
			System.out.println(ling);
		}
		reader.close();
	}

	public static void fileWriter() throws Exception{
		String str = "aaaadfksdlkfdsljfdslfds";
		char[] cs = new char[str.length()];
		str.getChars(0,str.length(),cs,0);

		BufferedWriter writer = new BufferedWriter(new FileWriter("source/out.txt"));
		writer.write(cs);
		writer.close();
	}

	public static void charreader() throws Exception{
		String str = "aaaadfksdlkfdsljfdslfds";
		char[] cs = new char[str.length()];
		str.getChars(0,str.length(),cs,0);
		CharArrayReader reader = new CharArrayReader(cs);
		int i = 0;
		while(-1 != (i=reader.read())){
			System.out.println((char) i);
		}
		reader.close();
	}

	public static void randomfile() throws Exception{
		RandomAccessFile raf = new RandomAccessFile(new File("source/out.txt"),"rw");
		raf.writeInt(42);
		raf.writeDouble(42.42);
		raf.seek(0);//移到文件开头
		System.out.println(raf.readInt());
		System.out.println(raf.readDouble());
		raf.close();
	}


}
